const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const tool = require("../../tools/tool");
const router = new Router();

router.post('/app/getGoods', async ctx => {
    let formData = ctx.request.body;
    let currentPage = ctx.request.body.currentPage?ctx.request.body.currentPage:1;
    let pageSize = ctx.request.body.pageSize?ctx.request.body.pageSize:30;
    let offset = (currentPage-1) *pageSize;
    let tenantId = ctx.user.tenantId;
    let searchTxt = tool.xe.trim(formData.searchTxt)
    let goodCatId = formData.goodCatId?formData.goodCatId:''
    let listSql = `select id,name,spec,unit1,unit2,price1,price2,categoryId,categoryName,barCode,barCode1,hjName,hjId,
      brandName,brandId,sendPrice from v_goods where tenantId=${tenantId} `
    if(goodCatId) {
        listSql += ` and categoryId=${goodCatId} `
    }
    if(searchTxt) {
        listSql += ` and(name like '%${searchTxt}%' or pinYin like '%${searchTxt}%' or barCode like '%${searchTxt}%' or barCode1 like '%${searchTxt}%') `;
    }
    listSql += ` order by id desc offset ${offset} rows fetch next ${pageSize} rows only;`
    let list = await db.query(listSql, {type: QueryTypes.SELECT})
    return ctx.suc('', {total:0, list })

})

module.exports = router;
